<?php
/*
 * @author Jens Beyer
 * @date 06-04-2012
 */
class User {
    public function isLoggedIn()
    {
        $sessionLoggedIn = false;
        if(isset($_SESSION['logged'])) {
            error_log("session logged \n");
            $q = 'SELECT userId, password FROM Users WHERE name="' . $_SESSION['name'] . '" AND password="' . $_SESSION['logged'] . '"';
            $row = $this->getDb()->queryArray($q);
            if($row !== false) {
                $sessionLoggedIn = true;
                $this->getAt()->assign('loggedIn', $sessionLoggedIn);
            }
        }

        return $sessionLoggedIn;
    }

    public function logout()
    {
        if(isset($_SESSION)) {
            session_destroy();
        }
    }

    /**
     * @return Smarty
     */
    public function getAt()
    {
        if (!$this->_at) {
            $this->_at = Controller::getActionTemplate("localhost");
        }
        return $this->_at;
    }

    /**
     *
     * @param Smarty $at
     */
    public function setAt(Smarty $at)
    {
        $this->_at = $at;
    }

    /**
     *
     * @return Db
     */
    public function getDb()
    {
        if (!$this->_db) {
            $this->_db = Controller::getDatabase("localhost");
        }
        return $this->_db;
    }

    /**
     *
     * @param Db $db
     */
    public function setDb(Db $db)
    {
        $this->_db = $db;
    }
}